import dayjs from 'dayjs'

export function formatDate(date, format) {
    // 日期不存在，则返回空
    if (!date) {
        return ''
    }
    // 日期存在，则进行格式化
    return date ? dayjs(date).format(format ?? 'YYYY-MM-DD HH:mm:ss') : ''
}
// 计算年龄
export function calculateAge(timestamp) {
    if (!timestamp || timestamp == 0) return '-'

    const birthDate = new Date(timestamp)
    const today = new Date()

    let age = today.getFullYear() - birthDate.getFullYear()
    const monthDiff = today.getMonth() - birthDate.getMonth()

    if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
        age--
    }

    return age
}